package com.baoli.umpush.util;

import com.baoli.umpush.common.UmengConstant;
import com.baoli.umpush.pojo.UmengRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Calendar;

@Component
public class UmengUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 广播下次推送时间
     */
    public Long broadcastNextPushTime() {
        UmengRedis umengRedis = null;
        long result = 0L;
        long curTime = this.getCurrentDayTimeLong(0);
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        if (redisTemplate.hasKey(UmengConstant.BROADCAST_KEY)) {//broadcast 广播，默认每天可推送10次
            umengRedis = (UmengRedis) operations.get(UmengConstant.BROADCAST_KEY);
            if (umengRedis.getTime() > curTime) {
                result = this.getTomorrowTimeDifference();//明天凌晨到当前的时间差
            } else {
                // 限制一天十次
                if (curTime != umengRedis.getTime()) {
                    // 不同天，初始化
                    umengRedis.setFrequency(1);
                    umengRedis.setTime(curTime);
                } else if (umengRedis.getFrequency() < UmengConstant.BROADCAST_TIMES) {
                    // 同天，发送次数加一
                    umengRedis.setFrequency(umengRedis.getFrequency() + 1);
                } else {
                    // 下一天再发
                    result = this.getTomorrowTimeDifference();
                    umengRedis.setFrequency(0);
                    umengRedis.setTime(this.getCurrentDayTimeLong(1));
                }
            }
        } else {
            // 第一次推送
            umengRedis = new UmengRedis();
            umengRedis.setFrequency(1);
            umengRedis.setTime(curTime);
        }

        operations.set(UmengConstant.BROADCAST_KEY, umengRedis);

        return result;
    }

    /**
     * long类型的时间，位数到天
     */
    private long getCurrentDayTimeLong(int days) {
        // 当前时间
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, days);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        return Long.valueOf("" + year + month + date);
    }

    /**
     * 明天凌晨到当前的时间差
     */
    private long getTomorrowTimeDifference() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTimeInMillis() - System.currentTimeMillis();
    }

    /**
     * 文件播下次推送时间
     */
    public Long filecastNextPushTime() {
        UmengRedis umengRedis = null;
        long result = 0L;
        long curTime = this.getCurrentHourTimeLong(0);
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        if (redisTemplate.hasKey(UmengConstant.FILECAST_KEY)) {
            umengRedis = (UmengRedis) operations.get(UmengConstant.FILECAST_KEY);
            if (umengRedis.getTime() > curTime) {
                result = this.getHourTimeDifference();
            } else {
                // 限制300次
                if (curTime != umengRedis.getTime()) {
                    // 不在同一个小时内，初始化
                    umengRedis.setFrequency(1);
                    umengRedis.setTime(curTime);
                } else if (umengRedis.getFrequency() < UmengConstant.FILECAST_TIMES) {
                    // 同一个小时内，发送次数加一
                    umengRedis.setFrequency(umengRedis.getFrequency() + 1);
                } else {
                    // 下一小时再发
                    result = this.getHourTimeDifference();
                    umengRedis.setFrequency(0);
                    umengRedis.setTime(this.getCurrentHourTimeLong(1));
                }
            }
        } else {
            // 第一次推送
            umengRedis = new UmengRedis();
            umengRedis.setFrequency(1);
            umengRedis.setTime(curTime);
        }

        operations.set(UmengConstant.FILECAST_KEY, umengRedis);

        return result;
    }

    /**
     * long类型的时间，位数到小时
     */
    private long getCurrentHourTimeLong(int days) {
        // 当前时间
        Calendar c = Calendar.getInstance();
        c.add(Calendar.HOUR_OF_DAY, days);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        return Long.valueOf("" + year + month + date + hour);
    }

    /**
     * 下一个小时到当前的时间差
     */
    private long getHourTimeDifference() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR_OF_DAY, 1);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTimeInMillis() - System.currentTimeMillis();
    }
}
